﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Thu Vien Them Vao
using QuanLyQuanCafe.BusinessObject;
using QuanLyQuanCafe.BusinessLogic;

namespace QuanLyQuanCafe
{
    public partial class FrmPhieuDatBan : Form
    {
        int trangthai;
        KhachHangBL khachhangBL = new KhachHangBL();
        BanBL banBL = new BanBL();
        PhieuDatBanBL phieudatbanBL = new PhieuDatBanBL();
        public FrmPhieuDatBan()
        {
            InitializeComponent();
        }

        private void FrmPhieuDatBan_Load(object sender, EventArgs e)
        {
            cboKhachHang.DataSource = khachhangBL.LayDanhSachKhachHang();
            cboKhachHang.ValueMember = "HoTen";
            cboKhachHang.DisplayMember = "HoTen";

            cboBan.DataSource = banBL.LayTatCaBan();
            cboBan.DisplayMember = "MaBan";
            cboBan.ValueMember = "MaBan";

            dataGridViewPhieuDatBan.DataSource = phieudatbanBL.LayDanhSachPhieuDatBan();

            AnHienButton(false);
            txtMaDatBan.Visible = false;
            lbMaDatBan.Visible = false;
        }

        private void cboKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            string hotenKH = cboKhachHang.Text;
            cboCMND.DataSource = khachhangBL.LayDanhSachKhachHangTheoHoTen(hotenKH);
            cboCMND.ValueMember = "CMND_KH";
            cboCMND.DisplayMember = "CMND_KH";
        }

        private void CapNhatText()
        {
            dateTimePickerThoiDiem.Value = DateTime.Now;
            txtTienCoc.Text = "0";
        }

        private void AnHienButton(bool flag)
        {
            btnDatBan.Enabled = !flag;
            btnSuaThongTin.Enabled = !flag;
            btnHuyDatBan.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;

            cboKhachHang.Enabled = flag;
            cboCMND.Enabled = flag;
            cboBan.Enabled = flag;
            dateTimePickerThoiDiem.Enabled = flag;
            txtTienCoc.Enabled = flag;

            dataGridViewPhieuDatBan.Enabled = !flag;
        }

        private PhieuDatBanBO LayThongTinDatBan()
        {
            PhieuDatBanBO phieudatbanBO = new PhieuDatBanBO();
            phieudatbanBO.HoTenKH = cboKhachHang.Text;
            phieudatbanBO.CMND_KH = cboCMND.Text;
            phieudatbanBO.ThoiDiem = Convert.ToDateTime(dateTimePickerThoiDiem.Value);
            phieudatbanBO.TienCoc = float.Parse(txtTienCoc.Text);
            phieudatbanBO.MaBan = int.Parse(cboBan.Text);
            phieudatbanBO.MaDB = int.Parse(txtMaDatBan.Text);
            return phieudatbanBO;
        }

        private void btnDatBan_Click(object sender, EventArgs e)
        {
            cboBan.DataSource = banBL.LayDanhSachBanTheoTinhTrang(1);            
            cboBan.DisplayMember = "MaBan";
            cboBan.ValueMember = "MaBan";

            AnHienButton(true);
            CapNhatText();            
            trangthai = 1;
            txtMaDatBan.Text = "0";
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {            
            AnHienButton(false);
            txtMaDatBan.Visible = false;
            lbMaDatBan.Visible = false;

            cboBan.DataSource = banBL.LayTatCaBan();
            cboBan.DisplayMember = "MaBan";
            cboBan.ValueMember = "MaBan";
        }

        private void btnSuaThongTin_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            txtMaDatBan.Visible = true;
            lbMaDatBan.Visible = true;
            cboBan.Enabled = false;
            cboCMND.Enabled = false;
            cboKhachHang.Enabled = false;

            trangthai = 2;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {            
            try
            {               
                PhieuDatBanBO phieudatbanBO = LayThongTinDatBan();
                if (trangthai == 1)
                {
                    //MessageBox.Show("123");
                    if (phieudatbanBL.ThemPhieuDatBan(phieudatbanBO))
                        FrmPhieuDatBan_Load(sender, e);
                }

                if (trangthai == 2)
                {
                    DataTable datatemp = banBL.LayDanhSachBanTheoMaBan(cboBan.Text);
                    if (int.Parse(datatemp.Rows[0]["TinhTrang"].ToString()) == 2)
                    {
                        //MessageBox.Show("123");
                        if (phieudatbanBL.SuaPhieuDatBan(phieudatbanBO))
                            FrmPhieuDatBan_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Phiếu Đặt Bàn Đã Hết Hiệu Lực. Không Thể Sửa Thông Tin.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Kiểm Tra Lại Các Thông Tin Đã Nhập.\n" + ex.Message, "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuyDatBan_Click(object sender, EventArgs e)
        {            
            DataTable datatemp = banBL.LayDanhSachBanTheoMaBan(cboBan.Text);
            if (int.Parse(datatemp.Rows[0]["TinhTrang"].ToString()) == 2)
            {
                txtMaDatBan.Visible = true;
                lbMaDatBan.Visible = true;
                PhieuDatBanBO phieudatbanBO = LayThongTinDatBan();
                if (MessageBox.Show("Bạn Có Muốn Hủy Đặt Bàn Của [" + cboKhachHang.Text + "] Vào Thời Điểm [" + dateTimePickerThoiDiem.Text + "] Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    if (phieudatbanBL.XoaPhieuDatBan(phieudatbanBO))
                        FrmPhieuDatBan_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Phiếu Đặt Bàn Đã Hết Hiệu Lực. Không Thể Hủy.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }

        private void dataGridViewPhieuDatBan_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try 
            {
                cboKhachHang.Text = dataGridViewPhieuDatBan.Rows[row].Cells["HoTenKH"].Value.ToString();
                cboCMND.Text = dataGridViewPhieuDatBan.Rows[row].Cells["CMND_KH"].Value.ToString();
                cboBan.Text = dataGridViewPhieuDatBan.Rows[row].Cells["MaBan"].Value.ToString();
                txtTienCoc.Text = dataGridViewPhieuDatBan.Rows[row].Cells["TienCoc"].Value.ToString();
                dateTimePickerThoiDiem.Text = dataGridViewPhieuDatBan.Rows[row].Cells["ThoiDiem"].Value.ToString();
                txtMaDatBan.Text = dataGridViewPhieuDatBan.Rows[row].Cells["MaDB"].Value.ToString();
            }
            catch { }
        }              
    }
}
